package org.admanager.acme.plugin.diagram.navigator;

import org.admanager.acme.plugin.diagram.edit.parts.AcmeRootEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.Component2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ComponentNameEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ComponentPort2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ComponentPortDelegatedPortsEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ComponentPortEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.Connector2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.Connector3EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorName2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorName3EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorNameEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorRole2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.ConnectorRoleDelegatedRolesEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.PortEditPart;
import org.admanager.acme.plugin.diagram.edit.parts.WrappingLabel2EditPart;
import org.admanager.acme.plugin.diagram.edit.parts.WrappingLabelEditPart;
import org.admanager.acme.plugin.diagram.part.AcmeADManagementMetamodelDiagramEditorPlugin;
import org.admanager.acme.plugin.diagram.part.AcmeADManagementMetamodelVisualIDRegistry;
import org.admanager.acme.plugin.diagram.providers.AcmeADManagementMetamodelElementTypes;
import org.admanager.acme.plugin.diagram.providers.AcmeADManagementMetamodelParserProvider;
import org.admanager.acmeadmanagement.plugin.acmeadmanagement.Port;
import org.admanager.acmeadmanagement.plugin.acmeadmanagement.Role;
import org.admanager.acmeadmanagement.plugin.acmeadmanagement.System;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class AcmeADManagementMetamodelNavigatorLabelProvider extends
		LabelProvider implements ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		AcmeADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		AcmeADManagementMetamodelDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof AcmeADManagementMetamodelNavigatorItem
				&& !isOwnView(((AcmeADManagementMetamodelNavigatorItem) element)
						.getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof AcmeADManagementMetamodelNavigatorGroup) {
			AcmeADManagementMetamodelNavigatorGroup group = (AcmeADManagementMetamodelNavigatorGroup) element;
			return AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof AcmeADManagementMetamodelNavigatorItem) {
			AcmeADManagementMetamodelNavigatorItem navigatorItem = (AcmeADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (AcmeADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case ComponentPortDelegatedPortsEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://acmeadmanagement/1.0?Binding", AcmeADManagementMetamodelElementTypes.Binding_4002); //$NON-NLS-1$
		case ComponentPort2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?System", AcmeADManagementMetamodelElementTypes.System_3003); //$NON-NLS-1$
		case ConnectorRole2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?Role", AcmeADManagementMetamodelElementTypes.Role_3001); //$NON-NLS-1$
		case AcmeRootEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://acmeadmanagement/1.0?System", AcmeADManagementMetamodelElementTypes.System_1000); //$NON-NLS-1$
		case ConnectorRoleDelegatedRolesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://acmeadmanagement/1.0?Attachment", AcmeADManagementMetamodelElementTypes.Attachment_4001); //$NON-NLS-1$
		case PortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?Port", AcmeADManagementMetamodelElementTypes.Port_3005); //$NON-NLS-1$
		case Component2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?Representation", AcmeADManagementMetamodelElementTypes.Representation_3002); //$NON-NLS-1$
		case ConnectorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://acmeadmanagement/1.0?Connector", AcmeADManagementMetamodelElementTypes.Connector_2001); //$NON-NLS-1$
		case ComponentPortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://acmeadmanagement/1.0?Component", AcmeADManagementMetamodelElementTypes.Component_2002); //$NON-NLS-1$
		case Connector3EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?Connector", AcmeADManagementMetamodelElementTypes.Connector_3006); //$NON-NLS-1$
		case Connector2EditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://acmeadmanagement/1.0?Component", AcmeADManagementMetamodelElementTypes.Component_3004); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = AcmeADManagementMetamodelDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null
				&& elementType != null
				&& AcmeADManagementMetamodelElementTypes
						.isKnownElementType(elementType)) {
			image = AcmeADManagementMetamodelElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof AcmeADManagementMetamodelNavigatorGroup) {
			AcmeADManagementMetamodelNavigatorGroup group = (AcmeADManagementMetamodelNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof AcmeADManagementMetamodelNavigatorItem) {
			AcmeADManagementMetamodelNavigatorItem navigatorItem = (AcmeADManagementMetamodelNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (AcmeADManagementMetamodelVisualIDRegistry.getVisualID(view)) {
		case ComponentPortDelegatedPortsEditPart.VISUAL_ID:
			return getBinding_4002Text(view);
		case ComponentPort2EditPart.VISUAL_ID:
			return getSystem_3003Text(view);
		case ConnectorRole2EditPart.VISUAL_ID:
			return getRole_3001Text(view);
		case AcmeRootEditPart.VISUAL_ID:
			return getSystem_1000Text(view);
		case ConnectorRoleDelegatedRolesEditPart.VISUAL_ID:
			return getAttachment_4001Text(view);
		case PortEditPart.VISUAL_ID:
			return getPort_3005Text(view);
		case Component2EditPart.VISUAL_ID:
			return getRepresentation_3002Text(view);
		case ConnectorEditPart.VISUAL_ID:
			return getConnector_2001Text(view);
		case ComponentPortEditPart.VISUAL_ID:
			return getComponent_2002Text(view);
		case Connector3EditPart.VISUAL_ID:
			return getConnector_3006Text(view);
		case Connector2EditPart.VISUAL_ID:
			return getComponent_3004Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getBinding_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getComponent_2002Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.Component_2002, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(ComponentNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSystem_1000Text(View view) {
		System domainModelElement = (System) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPort_3005Text(View view) {
		Port domainModelElement = (Port) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getConnector_2001Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.Connector_2001, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(ConnectorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRepresentation_3002Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.Representation_3002, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(WrappingLabelEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getConnector_3006Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.Connector_3006, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(ConnectorName3EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSystem_3003Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.System_3003, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(WrappingLabel2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getRole_3001Text(View view) {
		Role domainModelElement = (Role) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin
					.getInstance()
					.logError(
							"No domain element for view with visualID = " + 3001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getAttachment_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getComponent_3004Text(View view) {
		IParser parser = AcmeADManagementMetamodelParserProvider.getParser(
				AcmeADManagementMetamodelElementTypes.Component_3004, view
						.getElement() != null ? view.getElement() : view,
				AcmeADManagementMetamodelVisualIDRegistry
						.getType(ConnectorName2EditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			AcmeADManagementMetamodelDiagramEditorPlugin.getInstance()
					.logError("Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return AcmeRootEditPart.MODEL_ID
				.equals(AcmeADManagementMetamodelVisualIDRegistry
						.getModelID(view));
	}

}
